angular.module('baisonReport', ['baisonDb', 'baisonService'])
        .factory('Report', ['$location', '$rootScope', 'dbSelect', 'apiService', 'dbUpdate', 'panDian', 'ReceiptRecord', 'VIP', 'changeDate', 'timeStamp2String', function ($location, $rootScope, dbSelect, apiService, dbUpdate, panDian, ReceiptRecord, VIP, changeDate, timeStamp2String) {
                /**
                 * 热销商品报表
                 * @param day 1, 7, 30 按天数搜索
                 * @param search_type =num 按数量 =money 按金额
                 * @param callback
                 */
                var get_hot_goods = function (day, search_type) {
                    //baison_app.api.alert({msg:day,title:'day'});
                    var sql = ' select p.goods_name, p.goods_code, p.sku, sum(p.num) num, sum(p.money) money '
                            + ' from PosReceiptDetail p '
                            + ' inner join PosReceiptRecord r on r.record_code = p.record_code '
                            + ' where r.record_time >= ' + day + ' and r.is_return=0 and r.is_checkout=1 '
                            + ' group by p.sku order by sum(p.' + search_type + ') desc limit 10 ';
                    //baison_app.api.alert({msg:sql,title:'sql'});   
                    return dbSelect('YsPos', sql);

                };
                var get_hot_goods_by_time = function (start, end, search_type) {
                    var sql = ' select p.goods_name, p.goods_code, p.sku, sum(p.num) num, sum(p.money) money '
                            + ' from PosReceiptDetail p '
                            + ' inner join PosReceiptRecord r on r.record_code = p.record_code '
                            + ' where r.record_time >= ' + start + 'and r.record_time <' + end + ' and r.is_return=0 and r.is_checkout=1 '
                            + ' group by p.sku order by sum(p.' + search_type + ') desc limit 10 ';

                    return dbSelect('YsPos', sql);

                };
                var get_cash_by_daogou = function (day, search_type) {
                    var num = 0;
                    var money = 0.00;
                    var sql = ' select p.daogou_code,u.UserName, sum(p.num) num, sum(p.money) money '
                            + ' from PosReceiptDetail p '
                            + ' inner join PosReceiptRecord r on r.record_code = p.record_code '
                            + ' inner join PosUser u on u.UserCode = p.daogou_code '
                            + ' where   r.is_return=0 and r.is_checkout=1 ';
                    sql += ' group by p.daogou_code order by sum(p.' + search_type + ') desc ';

                    return dbSelect('YsPos', sql);
                };

                var get_cash_by_daogou_by_time = function (start, end, search_type) {
                    var num = 0;
                    var money = 0.00;
                    var sql = ' select p.daogou_code,u.UserName, sum(p.num) num, sum(p.money) money '
                            + ' from PosReceiptDetail p '
                            + ' inner join PosReceiptRecord r on r.record_code = p.record_code '
                            + ' inner join PosUser u on u.UserCode = p.daogou_code '
                            + ' where   r.is_return=0 and r.is_checkout=1 '


                    sql += ' and r.record_time >= "' + start + '"';


                    sql += ' and r.record_time <= "' + end + '"';

                    sql += ' group by p.daogou_code order by sum(p.' + search_type + ') desc ';

                    return dbSelect('YsPos', sql);


                };


                /**
                 * 进销存分析报表
                 */
                //进销存一览
                var get_Jxc_total = function (data) {
                    if (typeof data.record_time_start !== 'string' || typeof data.record_time_end !== 'string') {
                        data.record_time_start = timeStamp2String(data.record_time_start, 'datetime');
                        data.record_time_end = timeStamp2String(data.record_time_end, 'datetime');
                    }
                    var param = {
                        'store_code': $rootScope.current_shop.ShopCode,
                        'record_time_start': data.record_time_start,
                        'record_time_end': data.record_time_end,
                        'goods_code': data.goods_code,
                        'category_code': data.category_code,
                        'pageNum': data.pageNum
                    };
                    return apiService('report/store/total', param);
                };

                return {
                    get_hot_goods: get_hot_goods,
                    get_hot_goods_by_time: get_hot_goods_by_time,
                    get_cash_by_daogou: get_cash_by_daogou,
                    get_cash_by_daogou_by_time: get_cash_by_daogou_by_time,
                    get_Jxc_total: get_Jxc_total
                };


            }]);